package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.jwt.JwtUtil;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.mapper.SystemUserMapper;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wf.captcha.GifCaptcha;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private SystemUserService systemUserService;

    /**
     * 返回验证码
     * @return
     */
    @Override
    public CaptchaVo getCaptcha() {
        GifCaptcha captcha = new GifCaptcha(130, 48, 4);
        String image = captcha.toBase64();
        String code = captcha.text();

        String key = code+UUID.randomUUID().toString();

        redisTemplate.opsForValue().set(key,code,10, TimeUnit.MINUTES);

        CaptchaVo captchaVo = new CaptchaVo(image,key);
        captchaVo.setKey(key);
        captchaVo.setImage(image);
        return captchaVo;
    }

    /**
     * 登录验证
     *
     * @param loginVo
     * @return
     */
    @Override
    public String login(LoginVo loginVo) {

        //1，判断传过来的用户名和密码是否为空或者空串
        if (StringUtils.isEmpty(loginVo.getUsername()) || StringUtils.isEmpty(loginVo.getPassword())){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }
        //判断验证码是否存在
        if (redisTemplate.opsForValue().get(loginVo.getCaptchaKey())==null){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
        }

        //2判断验证码是否正确
        if (!loginVo.getCaptchaCode().equalsIgnoreCase(redisTemplate.opsForValue().get(loginVo.getCaptchaKey()))){
            throw  new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR);
        }
        //判断用户名是否可以查到用户信息
        SystemUser systemUser = systemUserService.getOne(new LambdaQueryWrapper<SystemUser>().eq(SystemUser::getUsername, loginVo.getUsername()));
        if (systemUser == null){
            throw  new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }

        //判断用户是否被禁用
        if (systemUser.getStatus() == BaseStatus.DISABLE){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR);
        }

        //验证密码是否正确
        if (!DigestUtils.sha256Hex(loginVo.getPassword()).equals(systemUser.getPassword())){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }

        String jwt = JwtUtil.createToken(systemUser.getId());
        return jwt;
    }
}
